package _03_recursion;

import org.junit.Test;

/**
 * @author: mornd
 * @dateTime: 2023/5/31 - 19:31
 * 单路递归求阶乘
 * n=1 特殊处理，否则 n = n * f(n-1)
 *
 *      1   2   3   4   5       6       7
 *      1   2   6   24  120     720     5040
 */
public class FactorialDemo {

    @Test
    public void test() {
        for (int i = 1; i <= 10; i++) {
            System.out.print(foo(i) + ",");
        }
    }

    /**
     * 倒叙输出字符串的字符
     */
    @Test
    public void testPrintString() {
        String str = "abcd";
        printChar(0, str);
    }

    private void printChar(int n, String str) {
        if(n  == str.length()) {
            return;
        }
        printChar(n + 1, str);
        System.out.println(str.charAt(n));
    };

    /**
     * 求 n 阶乘 要求 n > 0
     * @param n
     * @return
     */
    public int foo(int n) {
        if(n == 1) {
            return n;
        }
        return n * foo(n - 1);
    }
}
